Ext.namespace("GridUnitInfo");

GridUnitInfo.List=function(){
	this.txtUnitName = new Ext.form.TextField({
		width:100
	});
	this.txtUnitAddress = new Ext.form.TextField({
		width:150
	});

     this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查询',
        tooltip: {text: '查询营销单元信息'},
        handler: this.onSearch,
        scope: this
    });
	 this.btnAdd = new Ext.Toolbar.Button({
     	text:'增加营销单元', 
     	scope:this, 
     	iconCls:'icon-add', 
     	handler:this.onAdd
    });
    this.btnModify = new Ext.Toolbar.Button({
    	text:'修改营销单元', 
    	scope:this, 
    	iconCls:'icon-modify', 
    	handler:this.onModify
    });
    this.btnDelete = new Ext.Toolbar.Button({
    	text:'删除',
    	id :'deletePark',
    	disabled:true,
    	scope:this, 
    	iconCls:'icon-delete', 
    	handler:this.onDelete
    });
    this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url:'../gridunit/unitList.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'result',
			totalProperty: 'total',
			id: 'parkId',           
			fields: ['GRID_ID', 'UNIT_ID', 'UNIT_NAME', 'UNIT_TYPE', 'ADDRESS_DETAIL', 'GRID_NAME', 'USER_NAME']
		}),
		listeners:{
			 beforeload:{
				fn:function(thiz,options){
					Ext.apply(thiz.baseParams,{
						name :this.txtUnitName.getValue(),
						address :this.txtUnitAddress.getValue()
					});
				},
				scope:this
			}
		}
	});
	this.cm = new Ext.grid.ColumnModel({
		defaults:{
			width:40,
			sortable:true,
			align:'center'
		},
		columns:[
			new Ext.grid.RowNumberer(),
			{id:'gridId',dataIndex:'GRID_ID',hidden:true},
			{id:'unitId',dataIndex:'UNIT_ID',hidden:true},
			{header:'名称',dataIndex:'UNIT_NAME'},
			{header:'类型',dataIndex:'UNIT_TYPE',renderer:this.renderUnitType},
			{header:'地址',dataIndex:'ADDRESS_DETAIL'},
			{header:'所属网格',dataIndex:'GRID_NAME'},
			{header:'网格经理',dataIndex:'USER_NAME'}
		]
	});
	
	
	this.grid = new Ext.grid.GridPanel({
		title: '营销单元列表',
		region: 'center',
		store:this.store,
		height:580,
		cm:this.cm,
		loadMask: {msg: '加载中，请稍候...'},
		tbar: ['营销单元名称:',this.txtUnitName,'营销单元地址:',this.txtUnitAddress,this.btnSearch, '->',this.btnAdd,this.btnModify,this.btnDelete],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		loadMask: {msg:'加载中，请稍候...'},
		bbar:new Ext.PagingToolbar({
			pageSize:15,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条社区信息，一共 {2} 条',
			emptyMsg: '当前没有营销单元记录'
		}),
		viewConfig:{forceFit:true},
		listeners:{
	      	    rowdblclick : function( grid, rowIndex,e){
	      	    	var data = grid.store.getAt(rowIndex).data;
					GridUnitInfo.Detail.instance =new GridUnitInfo.Detail();
					gisEnable();//打开gis窗体
					GridUnitInfo.Detail.instance.initEdit(data);
					GridUnitInfo.Detail.instance.show();
				},
				scope:this				
	       }
	});
	GridUnitInfo.List.superclass.constructor.call(this,{
		resizable: false,
	    frame:false,	
		items:[this.grid]
	});
	this.store.load();
};

Ext.extend(GridUnitInfo.List,Ext.Viewport,{
	onSearch:function(){
		this.store.load({
			params:{
				start :0,
				limit :15,
				unitName :this.txtUnitName.getValue(),
				unitAddress :this.txtUnitAddress.getValue()
			}
		});
	},
	renderUnitType : function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.GRID_UNIT_TYPE[value][1];
		}
		return '';
	},
	onAdd:function(){
		GridUnitInfo.Detail.instance =new GridUnitInfo.Detail();
		gisEnable();//打开gis窗体
		GridUnitInfo.Detail.instance.initAdd();
		GridUnitInfo.Detail.instance.show();
	},
	onModify:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		var data=record.data;
		GridUnitInfo.Detail.instance =new GridUnitInfo.Detail();
		gisEnable();//打开gis窗体
		GridUnitInfo.Detail.instance.initEdit(data);
		GridUnitInfo.Detail.instance.show();
	},
	onDelete:function(){
		var record = this.grid.getSelectionModel().getSelected();
		if(record==null){
			Utils.warn('请选择一条记录！','警告');
			return;
		}
		Ext.MessageBox.confirm("提示","确认删除  '"+record.data.PARK_NAME+"' ？",function(btn){
            if(btn=="yes"){
				Ext.Ajax.request({
					url: '../GridUnitInfo/delete.form?timesnap=' + Date.parse(new Date()),
					method: 'POST',
					scope:this,
					params: {
						park_id: record.data.PARK_ID,
						gis_id : record.data.GIS_ID,
						address_id : record.data.ADDRESS_ID,
						unit_id : record.data.UNIT_ID,
						property_company : record.data.PROPERTY_COMPANY
					},
					success:function(resp){	
						this.store.reload();
					},
					failure:function(resp){	
						Utils.info('操作失败!','系统提示');
						return;
					}
				});
			}else{}
		},this);
	}
});

function gisEnable(){
	var connSyncSession = new Ext.data.Connection({
			url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
			method : 'POST'
		});
		Utils.request(connSyncSession, {
			success : function(resp, action) {
				var name = (resp.user)?resp.user.userId:'';
				if(name=="admin"){
					//Ext.getCmp("gisForm").disabled = false;
					Ext.getCmp("gisForm").enable();

				}
			}
		});
}

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget='side';
	Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
	GridUnitInfo.List.instance=new GridUnitInfo.List();
	GridUnitInfo.List.instance.show();
	var connSyncSession = new Ext.data.Connection({
		url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
		method : 'POST'
	});
	Utils.request(connSyncSession, {
		success : function(resp, action) {
			var name = (resp.user)?resp.user.userId:'';
			if(name=="admin"){
				Ext.getCmp("deletePark").enable();
				//Ext.getCmp("ParkGrid").render();
			}	
		}
	});
});
